package com.easy.model;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhuhaibo
 * @version 1.0
 * @date 2021/4/27 13:18
 */
@Slf4j
public class OpportunityTeamListener extends AnalysisEventListener<OpportunityTeam> {
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 5;
    List<OpportunityTeam> list = new ArrayList<>();


    @Override
    public void invoke(OpportunityTeam data, AnalysisContext context) {
//        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        data.setOutId(OpportunityTeam.DEFAULT_OUT_ID);
        data.setClassification(OpportunityTeam.DEFAULT_CLASSIFICATION);
        data.setCreatedEnterpriseId(OpportunityTeam.DEFAULT_SYSTEM_ID);
        data.setUpdatedEnterpriseId(OpportunityTeam.DEFAULT_SYSTEM_ID);
        data.setIsDeleted(OpportunityTeam.DEFAULT_DELETE);
        list.add(data);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
//        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
//        log.info("{}条数据，开始存储数据库！", list.size());
        list.forEach(opportunityTeam -> {
            String insertSql = "INSERT INTO crm_opportunity_team (`opportunity_id`,`enterprise_user_id`,`authority`,`performance`, `role`, " +
                    "`out_id`,`classification`,`created_enterprise_id`, `updated_enterprise_id`,`createdAt`,  `updatedAt`,`isDeleted`,`id`) VALUES (";
            String endSql = ");";
            Long opportunityId = opportunityTeam.getOpportunityId();
            Long enterpriseUserId = opportunityTeam.getEnterpriseUserId();
            String authority = opportunityTeam.getAuthority();
            Integer performance = opportunityTeam.getPerformance();
            String role = opportunityTeam.getRole();

            String outId = opportunityTeam.getOutId();
            String classification = opportunityTeam.getClassification();
            Long createdEnterpriseId = opportunityTeam.getCreatedEnterpriseId();
            Long updatedEnterpriseId = opportunityTeam.getUpdatedEnterpriseId();
            String createdAt = opportunityTeam.getCreatedAt();
            String updatedAt = opportunityTeam.getUpdatedAt();
            String isDeleted = opportunityTeam.getIsDeleted();
            Long id = opportunityTeam.getId();

            insertSql += "'" + opportunityId + "','" + enterpriseUserId + "','" + authority + "','" + performance + "','" + role +
                    "','" + outId + "','" + classification + "','" + createdEnterpriseId + "','" + updatedEnterpriseId +
                    "'," + createdAt + "," + updatedAt + ",'"
                    + isDeleted + "','" + id + "'" + endSql;
            System.out.println(insertSql);
        });
//        log.info("存储数据库成功！");
    }
}
